package BlockR.function;

import BlockR.util.Byte_SHIFT;
import BlockR.util.Byte_XOR;

import java.util.Random;

public class RandomNum {
    public static byte[] generate_random_num(int length) {
        Random random = new Random();
        byte[] random_num = new byte[length];
        random.nextBytes(random_num);
        return random_num;
    }

    public static byte[] generate_test_random_num1(int length) {
        byte[] random_num = new byte[length];
        for (int i = 0; i < random_num.length; i++) {
           /* if (file_num % 4 == 0) {
                random_num[file_num] = 1;
            } else {
                random_num[file_num] = 0;
            }*/
            random_num[i] = 1;
        }
        return random_num;
    }

    public static byte[] generate_test_random_num2(int length) {
        byte[] random_num = new byte[length];
        for (int i = 0; i < random_num.length; i++) {
            if (i % 4 == 0) {
                random_num[i] = 1;
            } else {
                random_num[i] = 0;
            }
//            random_num[file_num] = 0;
        }
        return random_num;
    }

    public static byte[] generate_block_random_num_1(byte[] cipher_x, byte[] random_num1, int x,int arraySize) {
        byte[] block_random_num_1 = Byte_SHIFT.rightShift(random_num1, x,arraySize);   //R1>>X
        block_random_num_1 = Byte_XOR.XOR(cipher_x, block_random_num_1);    //Cx^(R1>>X)
        return block_random_num_1;
    }

    public static byte[] generate_block_random_num_2(byte[] cipher_y, byte[] random_num2, int y,int arraySize) {
        byte[] block_random_num_2;
        if (y % 2 == 0) { //偶数
            block_random_num_2 = Byte_SHIFT.leftShift(random_num2, y,arraySize);
        } else {  //奇数
            block_random_num_2 = Byte_SHIFT.rightShift(random_num2, y,arraySize);
        }
        block_random_num_2 = Byte_XOR.XOR(cipher_y, block_random_num_2);
        return block_random_num_2;
    }

    public static byte[] generate_index_random_num_1(byte[] random_num1, int shift_x,int arraySize) {
        byte[] index_random_num_1;
        index_random_num_1 = Byte_SHIFT.rightShift(random_num1, shift_x,arraySize);
        return index_random_num_1;
//        return generate_test_random_num1(128);
    }

    public static byte[] generate_index_random_num_2(byte[] random_num2, int shift_x,int arraySize) {
        byte[] index_random_num_2;
        if (shift_x % 2 == 0) {
            index_random_num_2 = Byte_SHIFT.leftShift(random_num2, shift_x,arraySize);//R2j=R2<<shift_X
        } else {
            index_random_num_2 = Byte_SHIFT.rightShift(random_num2, shift_x,arraySize);//R2j=R2>>shift_X
        }
        return index_random_num_2;
//        return generate_test_random_num1(128);
    }
}
